﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    [Serializable]
    public class D_HRM_PR_PayrollDtls : IDAL_HRM_PR_PayrollDtls
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_HRM_PR_PayrollDtls()
        {

        }

        public IList<M_HRM_PR_PayrollDtls> CalculateTax(Nullable<int> emp_type_seq
            , Nullable<int> pay_year, Nullable<int> pay_month)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_PayrollDtls> infoList = new List<M_HRM_PR_PayrollDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@emp_type_seq", emp_type_seq);
                dbManager.AddParameters(2, "@pay_year", pay_year);
                dbManager.AddParameters(3, "@pay_month", pay_month);

                strSQL = "SELECT pr.pay_seq, iprd.emp_seq"
                    + ", pr.emp_type_seq, pr.pay_year, pr.pay_month"
                    + ", coalesce(iprd.income_amount, 0) as income_amount"
                    + ", coalesce(dprd.deduct_amount, 0) as deduct_amount"
                    + ", coalesce(iprd.income_amount, 0) - coalesce(dprd.deduct_amount, 0) AS total_amount"

                    + ", CONCAT(np.prefix_name, emp.first_name, ' ', emp.last_name) as emp_name"
                    + ", emp.working_job_site_seq, emp.pos_seq, emp.class_seq"

                    //+ ", j.job_site_gfmis, j.job_site_name_gfmis"

                    + ", pos.pos_ref, pos.pos_name"

                    + ", cl.class_name"
                    + " FROM hrm_pr_payrolls pr"
                    + " LEFT JOIN (SELECT prd.pay_seq, prd.emp_seq, ext.ext_flag"
                        + ", SUM(COALESCE(prd.amount_pay, 0)) AS income_amount"
                        + " FROM hrm_pr_payroll_dtls prd"
                        + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                        + " WHERE prd.record_status not like @delete_status"
                        + " AND ext.ext_flag = 'I'"
                        + " GROUP BY prd.pay_seq, prd.emp_seq, ext.ext_flag"
                        + ") iprd ON pr.pay_seq = iprd.pay_seq"
                    + " LEFT JOIN (SELECT prd.pay_seq, prd.emp_seq, ext.ext_flag"
                        + ", SUM(COALESCE(prd.amount_pay, 0)) AS deduct_amount"
                        + " FROM hrm_pr_payroll_dtls prd"
                        + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                        + " WHERE prd.record_status not like @delete_status"
                        + " AND ext.ext_flag = 'D'"
                        + " GROUP BY prd.pay_seq, prd.emp_seq, ext.ext_flag"
                        + ") dprd ON pr.pay_seq = dprd.pay_seq"
                            + " and dprd.emp_seq = iprd.emp_seq"
                    + " LEFT JOIN hrm_ps_employees emp ON iprd.emp_seq = emp.emp_seq"
                    + " LEFT JOIN mas_name_prefixes np ON emp.name_prefix_seq = np.name_prefix_seq"
                    + " LEFT JOIN hrm_mp_positions pos ON emp.pos_seq = pos.pos_seq"
                    + " LEFT JOIN hrm_mp_classes cl ON emp.class_seq = cl.class_seq"
                    + " WHERE pr.record_status not like @delete_status"
                    + (emp_type_seq != null ? " AND pr.emp_type_seq = @emp_type_seq" : "")
                    + (pay_year != null ? " AND pr.pay_year = @pay_year" : "")
                    + (pay_month != null ? " AND pr.pay_month = @pay_month" : "")
                    + " ORDER BY CONVERT(COALESCE(pos.pos_ref, '0'), DECIMAL)";

                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_PayrollDtls info = new M_HRM_PR_PayrollDtls();
                        info.pay_seq = Convert.ToInt32(drc[i]["pay_seq"]);
                        info.emp_seq = cn.ConvertNullInt(drc[i]["emp_seq"]);
                        info.emp_type_seq = cn.ConvertNullInt(drc[i]["emp_type_seq"]);
                        info.pay_year = cn.ConvertNullInt(drc[i]["pay_year"]);
                        info.pay_month = cn.ConvertNullInt(drc[i]["pay_month"]);
                        info.income_amount = cn.ConvertNullDouble(drc[i]["income_amount"]);
                        info.deduct_amount = cn.ConvertNullDouble(drc[i]["deduct_amount"]);
                        info.total_amount = cn.ConvertNullDouble(drc[i]["total_amount"]);
                        info.emp_name = drc[i]["emp_name"].ToString();
                        info.working_job_site_seq = cn.ConvertNullInt(drc[i]["working_job_site_seq"]);
                        info.pos_seq = cn.ConvertNullInt(drc[i]["pos_seq"]);
                        info.class_seq = cn.ConvertNullInt(drc[i]["class_seq"]);
                        info.pos_ref = drc[i]["pos_ref"].ToString();
                        info.pos_name = drc[i]["pos_name"].ToString();
                        info.class_name = drc[i]["class_name"].ToString();

                        int working_job_site_seq = (info.working_job_site_seq == null ? -1 : info.working_job_site_seq.Value);
                        IDAL_HRM_MP_Jobsites jobsite = new D_HRM_MP_Jobsites();
                        M_HRM_MP_Jobsites jobsiteInfo = jobsite.GetGFMISInfo(working_job_site_seq);

                        info.job_site_gfmis = jobsiteInfo.job_site_gfmis;
                        info.job_site_name_gfmis = jobsiteInfo.job_site_name;

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_PR_PayrollDtls> GetSummary(Nullable<int> emp_type_seq
            , Nullable<int> job_site_gfmis, Nullable<int> pay_year, Nullable<int> pay_month)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_PayrollDtls> infoList = new List<M_HRM_PR_PayrollDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@emp_type_seq", emp_type_seq);
                //dbManager.AddParameters(2, "@job_site_gfmis", job_site_gfmis);
                dbManager.AddParameters(2, "@pay_year", pay_year);
                dbManager.AddParameters(3, "@pay_month", pay_month);

                strSQL = "SELECT pr.pay_seq, iprd.emp_seq, iprd.job_site_seq"
                         + ", pr.emp_type_seq, pr.pay_year, pr.pay_month"
                         + ", coalesce(iprd.income_amount, 0) as income_amount"
                         + ", coalesce(dprd.deduct_amount, 0) as deduct_amount"
                         + ", coalesce(iprd.income_amount, 0) - coalesce(dprd.deduct_amount, 0) AS total_amount"

                         + ", CONCAT(np.prefix_name, emp.first_name, ' ', emp.last_name) as emp_name"
                         + ", emp.working_job_site_seq, emp.pos_seq, emp.class_seq"

                         //+ ", j.job_site_gfmis, j.job_site_name_gfmis"

                         + ", pos.pos_ref, pos.pos_name, j.job_site_name"
                         //+ ",  j.job_site_gfmis"

                         + ", cl.class_name"
                         + " FROM hrm_pr_payrolls pr"
                         + " LEFT JOIN (SELECT prd.pay_seq, prd.emp_seq, prd.job_site_seq, ext.ext_flag"
                         + ", SUM(COALESCE(prd.amount_pay, 0)) AS income_amount"
                         + " FROM hrm_pr_payroll_dtls prd"
                         + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                         + " WHERE prd.record_status not like @delete_status"
                         + " AND ext.ext_flag = 'I'"
                         + " GROUP BY prd.pay_seq, prd.emp_seq, prd.job_site_seq, ext.ext_flag"
                         + ") iprd ON pr.pay_seq = iprd.pay_seq"
                         + " LEFT JOIN (SELECT prd.pay_seq, prd.emp_seq, prd.job_site_seq, ext.ext_flag"
                         + ", SUM(COALESCE(prd.amount_pay, 0)) AS deduct_amount"
                         + " FROM hrm_pr_payroll_dtls prd"
                         + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                         + " WHERE prd.record_status not like @delete_status"
                         + " AND ext.ext_flag = 'D'"
                         + " GROUP BY prd.pay_seq, prd.emp_seq, prd.job_site_seq, ext.ext_flag"
                         + ") dprd ON pr.pay_seq = dprd.pay_seq"
                         + " and dprd.emp_seq = iprd.emp_seq"
                         + " LEFT JOIN hrm_ps_employees emp ON iprd.emp_seq = emp.emp_seq"
                         + " LEFT JOIN mas_name_prefixes np ON emp.name_prefix_seq = np.name_prefix_seq"
                         + " LEFT JOIN hrm_mp_positions pos ON emp.pos_seq = pos.pos_seq"
                         + " LEFT JOIN hrm_mp_classes cl ON emp.class_seq = cl.class_seq"
                         + " LEFT JOIN hrm_mp_job_sites j ON iprd.job_site_seq = j.job_site_seq"
                         + " WHERE pr.record_status not like @delete_status"
                         + (emp_type_seq != null ? " AND pr.emp_type_seq = @emp_type_seq" : "")
                         + (pay_year != null ? " AND pr.pay_year = @pay_year" : "")
                         + (pay_month != null ? " AND pr.pay_month = @pay_month" : "")
                         + " ORDER BY pos.pos_ref;";
                    //+ " CONVERT(COALESCE(pos.pos_ref, '0'), DECIMAL) ";

                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_PayrollDtls info = new M_HRM_PR_PayrollDtls();
                        info.pay_seq = Convert.ToInt32(drc[i]["pay_seq"]);
                        info.emp_seq = cn.ConvertNullInt(drc[i]["emp_seq"]);
                        info.job_site_seq = cn.ConvertNullInt(drc[i]["job_site_seq"]);
                        info.emp_type_seq = cn.ConvertNullInt(drc[i]["emp_type_seq"]);
                        info.pay_year = cn.ConvertNullInt(drc[i]["pay_year"]);
                        info.pay_month = cn.ConvertNullInt(drc[i]["pay_month"]);
                        info.income_amount = cn.ConvertNullDouble(drc[i]["income_amount"]);
                        info.deduct_amount = cn.ConvertNullDouble(drc[i]["deduct_amount"]);
                        info.total_amount = cn.ConvertNullDouble(drc[i]["total_amount"]);
                        info.emp_name = drc[i]["emp_name"].ToString();
                        info.working_job_site_seq = cn.ConvertNullInt(drc[i]["working_job_site_seq"]);
                        info.pos_seq = cn.ConvertNullInt(drc[i]["pos_seq"]);
                        info.class_seq = cn.ConvertNullInt(drc[i]["class_seq"]);
                        info.pos_ref = drc[i]["pos_ref"].ToString();
                        info.pos_name = drc[i]["pos_name"].ToString();
                        info.class_name = drc[i]["class_name"].ToString();

                        //info.job_site_gfmis = drc[i]["job_site_gfmis"].ToString();
                        info.job_site_name_gfmis = drc[i]["job_site_name"].ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_PR_PayrollDtls> GetFull(Nullable<int> emp_seq, string ext_flag, Nullable<int> pay_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_PayrollDtls> infoList = new List<M_HRM_PR_PayrollDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@emp_seq", emp_seq);
                dbManager.AddParameters(2, "@ext_flag", ext_flag);
                dbManager.AddParameters(3, "@pay_seq", pay_seq);

                strSQL = "SELECT prd.pay_dtls_seq, prd.pay_seq, prd.emp_seq, prd.job_site_seq"
                    + ", prd.ext_seq, prd.amount, prd.amount_pay, prd.remarks"
                    + ", prd.money_req_dtls_seq"

                    + ", pr.emp_type_seq, pr.pay_year, pr.pay_month"

                    + ", ext.ext_code, ext.ext_name, ext.ext_name_short, ext.ext_flag, ext.ext_tax, ext.ext_monthly"

                    //+ ", CONCAT(np.prefix_name, emp.first_name, ' ', emp.last_name) as emp_name"
                    //+ ", emp.working_job_site_seq, emp.pos_seq, emp.class_seq"

                    //+ ", j.job_site_gfmis, j.job_site_name_gfmis"

                    //+ ", pos.pos_ref, pos.pos_name"

                    //+ ", cl.class_name"

                    + ", prd.record_status, prd.created_by, prd.created_when, prd.updated_by, prd.updated_when"
                    + " FROM hrm_pr_payroll_dtls prd"
                    + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                    + " LEFT JOIN hrm_pr_payrolls pr ON prd.pay_seq = pr.pay_seq"
                    //+ " LEFT JOIN hrm_ps_employee_types et ON pr.emp_type_seq = et.emp_type_seq"
                    //+ " LEFT JOIN hrm_ps_employees emp ON prd.emp_seq = emp.emp_seq"
                    //+ " LEFT JOIN mas_name_prefixes np ON emp.name_prefix_seq = np.name_prefix_seq"
                    //+ " LEFT JOIN hrm_mp_positions pos ON emp.pos_seq = pos.pos_seq"
                    //+ " LEFT JOIN hrm_mp_classes cl ON emp.class_seq = cl.class_seq"
                    + " WHERE prd.record_status not like @delete_status"
                    + (emp_seq != null ? " AND prd.emp_seq = @emp_seq" : "")
                    + (ext_flag != null ? " AND ext.ext_flag = @ext_flag" : "")
                    + (pay_seq != null ? " AND prd.pay_seq = @pay_seq" : "")
                    ;
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_PayrollDtls info = new M_HRM_PR_PayrollDtls();
                        info.pay_dtls_seq = Convert.ToInt32(drc[i]["pay_dtls_seq"]);
                        info.pay_seq = cn.ConvertNullInt(drc[i]["pay_seq"]);
                        info.emp_seq = cn.ConvertNullInt(drc[i]["emp_seq"]);
                        info.ext_seq = cn.ConvertNullInt(drc[i]["ext_seq"]);
                        info.amount = cn.ConvertNullDouble(drc[i]["amount"]);
                        info.amount_pay = cn.ConvertNullDouble(drc[i]["amount_pay"]);
                        info.remarks = drc[i]["remarks"].ToString();
                        info.money_req_dtls_seq = cn.ConvertNullInt(drc[i]["money_req_dtls_seq"]);

                        info.emp_type_seq = cn.ConvertNullInt(drc[i]["emp_type_seq"]);
                        info.pay_year = cn.ConvertNullInt(drc[i]["pay_year"]);
                        info.pay_month = cn.ConvertNullInt(drc[i]["pay_month"]);
                        //info.emp_type_name = drc[i]["emp_type_name"].ToString();
                        info.ext_code = drc[i]["ext_code"].ToString();
                        info.ext_name = drc[i]["ext_name"].ToString();
                        info.ext_name_short = drc[i]["ext_name_short"].ToString();
                        info.ext_flag = drc[i]["ext_flag"].ToString();
                        info.ext_tax = drc[i]["ext_tax"].ToString();
                        info.ext_monthly = drc[i]["ext_monthly"].ToString();
                        //info.emp_name = drc[i]["emp_name"].ToString();
                        //info.working_job_site_seq = cn.ConvertNullInt(drc[i]["working_job_site_seq"]);
                        //info.pos_seq = cn.ConvertNullInt(drc[i]["pos_seq"]);
                        //info.class_seq = cn.ConvertNullInt(drc[i]["class_seq"]);
                        //info.pos_ref = drc[i]["pos_ref"].ToString();
                        //info.pos_name = drc[i]["pos_name"].ToString();
                        //info.class_name = drc[i]["class_name"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_PR_PayrollDtls> GetSummaryByJobsiteGFMIS(Nullable<int> pay_seq
            , Nullable<int> ext_seq, string job_site_gfmis)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_PayrollDtls> infoList = new List<M_HRM_PR_PayrollDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@pay_seq", pay_seq);
                dbManager.AddParameters(2, "@ext_seq", ext_seq);
                dbManager.AddParameters(3, "@job_site_gfmis", job_site_gfmis);

                strSQL = "SELECT prd.pay_seq, j.job_site_gfmis, prd.ext_seq, prd.money_req_dtls_seq"
                    + ", sum(coalesce(prd.amount_pay, 0)) as amount_pay"

                    + " FROM hrm_pr_payroll_dtls prd"
                    + " LEFT JOIN hrm_mp_job_sites j ON prd.job_site_seq = j.job_site_seq"
                    + " WHERE prd.record_status not like @delete_status"
                    + (pay_seq != null ? " AND prd.pay_seq = @pay_seq" : "")
                    + (ext_seq != null ? " AND prd.ext_seq = @ext_seq" : "")
                    + (job_site_gfmis != null ? " AND j.job_site_gfmis = @job_site_gfmis" : "")
                    + " GROUP BY prd.pay_seq, j.job_site_gfmis, prd.ext_seq, prd.money_req_dtls_seq";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_PayrollDtls info = new M_HRM_PR_PayrollDtls();
                        info.pay_seq = cn.ConvertNullInt(drc[i]["pay_seq"]);
                        info.ext_seq = cn.ConvertNullInt(drc[i]["ext_seq"]);
                        info.amount_pay = cn.ConvertNullDouble(drc[i]["amount_pay"]);
                        info.money_req_dtls_seq = cn.ConvertNullInt(drc[i]["money_req_dtls_seq"]);
                        info.job_site_gfmis = drc[i]["job_site_gfmis"].ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_PR_PayrollDtls> GetFullFromMoneyReqSeq(Nullable<int> money_req_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_PayrollDtls> infoList = new List<M_HRM_PR_PayrollDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@money_req_seq", money_req_seq);

                strSQL = "SELECT prd.pay_dtls_seq, prd.pay_seq, prd.emp_seq, prd.job_site_seq"
                    + ", prd.ext_seq, prd.amount, prd.amount_pay, prd.remarks"
                    + ", prd.money_req_dtls_seq"

                    + ", pr.emp_type_seq, pr.pay_year, pr.pay_month"

                    + ", ext.ext_code, ext.ext_name, ext.ext_name_short, ext.ext_flag, ext.ext_tax, ext.ext_monthly"

                    + ", prd.record_status, prd.created_by, prd.created_when, prd.updated_by, prd.updated_when"
                    + " FROM hrm_pr_payroll_dtls prd"
                    + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                    + " LEFT JOIN hrm_pr_payrolls pr ON prd.pay_seq = pr.pay_seq"
                    + " LEFT JOIN fim_money_request_dtls mrd ON prd.money_req_dtls_seq = mrd.money_req_dtls_seq"
                    + " WHERE prd.record_status not like @delete_status"
                    + (money_req_seq != null ? " AND mrd.money_req_seq = @money_req_seq" : "")
                    ;
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_PayrollDtls info = new M_HRM_PR_PayrollDtls();
                        info.pay_dtls_seq = Convert.ToInt32(drc[i]["pay_dtls_seq"]);
                        info.pay_seq = cn.ConvertNullInt(drc[i]["pay_seq"]);
                        info.emp_seq = cn.ConvertNullInt(drc[i]["emp_seq"]);
                        info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"]);
                        info.ext_seq = cn.ConvertNullInt(drc[i]["ext_seq"]);
                        info.amount = cn.ConvertNullDouble(drc[i]["amount"]);
                        info.amount_pay = cn.ConvertNullDouble(drc[i]["amount_pay"]);
                        info.remarks = drc[i]["remarks"].ToString();
                        info.money_req_dtls_seq = cn.ConvertNullInt(drc[i]["money_req_dtls_seq"]);

                        info.emp_type_seq = cn.ConvertNullInt(drc[i]["emp_type_seq"]);
                        info.pay_year = cn.ConvertNullInt(drc[i]["pay_year"]);
                        info.pay_month = cn.ConvertNullInt(drc[i]["pay_month"]);
                        info.ext_code = drc[i]["ext_code"].ToString();
                        info.ext_name = drc[i]["ext_name"].ToString();
                        info.ext_name_short = drc[i]["ext_name_short"].ToString();
                        info.ext_flag = drc[i]["ext_flag"].ToString();
                        info.ext_tax = drc[i]["ext_tax"].ToString();
                        info.ext_monthly = drc[i]["ext_monthly"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_HRM_PR_PayrollDtls GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_HRM_PR_PayrollDtls info = new M_HRM_PR_PayrollDtls();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT prd.pay_dtls_seq, prd.pay_seq, prd.emp_seq, prd.job_site_seq, prd.ext_seq"
                    + ", prd.amount, prd.amount_pay, prd.remarks, prd.money_req_dtls_seq"

                    + ", pr.emp_type_seq, pr.pay_year, pr.pay_month"

                    + ", et.emp_type_name"

                    + ", ext.ext_code, ext.ext_name, ext.ext_name_short, ext.ext_flag, ext.ext_tax, ext.ext_monthly"

                    + ", CONCAT(np.prefix_name, emp.first_name, ' ', emp.last_name) as emp_name"
                    + ", emp.working_job_site_seq, emp.pos_seq, emp.class_seq"

                    + ", j.job_site_gfmis, j.job_site_name_gfmis"

                    + ", pos.pos_ref, pos.pos_name"

                    + ", cl.class_name"

                    + ", prd.record_status, prd.created_by, prd.created_when, prd.updated_by, prd.updated_when"
                    + " FROM hrm_pr_payroll_dtls prd"
                    + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                    + " LEFT JOIN hrm_pr_payrolls pr ON prd.pay_seq = pr.pay_seq"
                    + " LEFT JOIN hrm_ps_employee_types et ON pr.emp_type_seq = et.emp_type_seq"
                    + " LEFT JOIN hrm_ps_employees emp ON prd.emp_seq = emp.emp_seq"
                    + " LEFT JOIN mas_name_prefixes np ON emp.name_prefix_seq = np.name_prefix_seq"
                    + " LEFT JOIN hrm_mp_positions pos ON emp.pos_seq = pos.pos_seq"
                    + " LEFT JOIN hrm_mp_classes cl ON emp.class_seq = cl.class_seq"
                    + " where prd.pay_dtls_seq = @seq";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.pay_dtls_seq = Convert.ToInt32(drc[0]["pay_dtls_seq"]);
                    info.pay_seq = cn.ConvertNullInt(drc[0]["pay_seq"]);
                    info.emp_seq = cn.ConvertNullInt(drc[0]["emp_seq"]);
                    info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"]);
                    info.ext_seq = cn.ConvertNullInt(drc[0]["ext_seq"]);
                    info.amount = cn.ConvertNullDouble(drc[0]["amount"]);
                    info.amount_pay = cn.ConvertNullDouble(drc[0]["amount_pay"]);
                    info.remarks = drc[0]["remarks"].ToString();
                    info.money_req_dtls_seq = cn.ConvertNullInt(drc[0]["money_req_dtls_seq"]);

                    info.emp_type_seq = cn.ConvertNullInt(drc[0]["emp_type_seq"]);
                    info.pay_year = cn.ConvertNullInt(drc[0]["pay_year"]);
                    info.pay_month = cn.ConvertNullInt(drc[0]["pay_month"]);
                    info.emp_type_name = drc[0]["emp_type_name"].ToString();
                    info.ext_code = drc[0]["ext_code"].ToString();
                    info.ext_name = drc[0]["ext_name"].ToString();
                    info.ext_name_short = drc[0]["ext_name_short"].ToString();
                    info.ext_flag = drc[0]["ext_flag"].ToString();
                    info.ext_tax = drc[0]["ext_tax"].ToString();
                    info.ext_monthly = drc[0]["ext_monthly"].ToString();
                    info.emp_name = drc[0]["emp_name"].ToString();
                    info.working_job_site_seq = cn.ConvertNullInt(drc[0]["working_job_site_seq"]);
                    info.pos_seq = cn.ConvertNullInt(drc[0]["pos_seq"]);
                    info.class_seq = cn.ConvertNullInt(drc[0]["class_seq"]);
                    info.pos_ref = drc[0]["pos_ref"].ToString();
                    info.pos_name = drc[0]["pos_name"].ToString();
                    info.class_name = drc[0]["class_name"].ToString();

                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public string GetPaySeq(Nullable<int> emp_type_seq, Nullable<int> pay_year, Nullable<int> pay_month)
        {
            string strSQL;
            string strPaySeq = null;
            DataRowCollection drc = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@emp_type_seq", emp_type_seq);
                dbManager.AddParameters(2, "@pay_year", pay_year);
                dbManager.AddParameters(3, "@pay_month", pay_month);

                strSQL = "SELECT pay_seq"
                    + " FROM hrm_pr_payrolls"
                    + " WHERE record_status not like @delete_status"
                    + (emp_type_seq != null ? " AND emp_type_seq = @emp_type_seq" : "")
                    + (pay_year != null ? " AND pay_year = @pay_year" : "")
                    + (pay_month != null ? " AND pay_month = @pay_month" : "");
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    strPaySeq = drc[0]["pay_seq"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return strPaySeq;
        }

        public void Insert(M_HRM_PR_PayrollDtls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(11);

                dbManager.AddParameters(0, "@pay_dtls_seq", info.pay_dtls_seq);
                dbManager.AddParameters(1, "@pay_seq", info.pay_seq);
                dbManager.AddParameters(2, "@emp_seq", info.emp_seq);
                dbManager.AddParameters(3, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(4, "@ext_seq", info.ext_seq);
                dbManager.AddParameters(5, "@amount", info.amount);
                dbManager.AddParameters(6, "@amount_pay", info.amount_pay);
                dbManager.AddParameters(7, "@remarks", info.remarks);

                dbManager.AddParameters(8, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(9, "@created_by", info.created_by);
                dbManager.AddParameters(10, "@created_when", info.created_when);

                strSQL = "INSERT INTO hrm_pr_payroll_dtls"
                    + "(pay_seq, emp_seq, job_site_seq, ext_seq, amount, amount_pay, remarks"
                    + ", record_status, created_by, created_when)"
                    + " VALUES(@pay_seq"
                        + ", @emp_seq"
                        + ", @job_site_seq"
                        + ", @ext_seq"
                        + ", @amount"
                        + ", @amount_pay"
                        + ", @remarks"

                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when)";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_HRM_PR_PayrollDtls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(10);

                dbManager.AddParameters(0, "@pay_dtls_seq", info.pay_dtls_seq);
                dbManager.AddParameters(1, "@pay_seq", info.pay_seq);
                dbManager.AddParameters(2, "@emp_seq", info.emp_seq);
                dbManager.AddParameters(3, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(4, "@ext_seq", info.ext_seq);
                dbManager.AddParameters(5, "@amount", info.amount);
                dbManager.AddParameters(6, "@amount_pay", info.amount_pay);
                dbManager.AddParameters(7, "@remarks", info.remarks);

                dbManager.AddParameters(8, "@updated_by", info.updated_by);
                dbManager.AddParameters(9, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_pr_payroll_dtls"
                    + " set pay_seq = @pay_seq"
                    + ", emp_seq = @emp_seq"
                    + ", job_site_seq = @job_site_seq"
                    + ", ext_seq = @ext_seq"
                    + ", amount = @amount"
                    + ", amount_pay = @amount_pay"
                    + ", remarks = @remarks"

                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE pay_dtls_seq = @pay_dtls_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_HRM_PR_PayrollDtls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@pay_dtls_seq", info.pay_dtls_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_pr_payroll_dtls"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE pay_dtls_seq = @pay_dtls_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void UpdateMoneyReqDtlsSeq(Nullable<int> pay_seq, Nullable<int> job_site_seq
            , Nullable<int> money_req_dtls_seq, Nullable<int> emp_seq)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@pay_seq", pay_seq);
                dbManager.AddParameters(1, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(2, "@money_req_dtls_seq", money_req_dtls_seq);
                dbManager.AddParameters(3, "@approve_status", SystemInfo._ApproveRecordStatus);
                dbManager.AddParameters(4, "@active_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@updated_by", emp_seq);
                dbManager.AddParameters(6, "@updated_when", DateTime.Now);

                // ======================= clear data =========================
                strSQL = "UPDATE hrm_pr_payroll_dtls"
                    + " SET money_req_dtls_seq = null"
                    //+ ", record_status = @active_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE money_req_dtls_seq = @money_req_dtls_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);

                // ======================= update data =========================
                strSQL = "UPDATE hrm_pr_payroll_dtls"
                    + " SET money_req_dtls_seq = @money_req_dtls_seq"
                    //+ ", record_status = @active_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE pay_seq = @pay_seq"
                    + (job_site_seq != null ? " AND job_site_seq = @job_site_seq" : "");
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void ClearMoneyReqDtlsSeq(Nullable<int> pay_seq, Nullable<int> job_site_seq
            , Nullable<int> money_req_dtls_seq, Nullable<int> emp_seq)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@pay_seq", pay_seq);
                dbManager.AddParameters(1, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(2, "@money_req_dtls_seq", money_req_dtls_seq);
                dbManager.AddParameters(4, "@active_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@updated_by", emp_seq);
                dbManager.AddParameters(6, "@updated_when", DateTime.Now);

                // ======================= clear data =========================
                strSQL = "UPDATE hrm_pr_payroll_dtls"
                    + " SET money_req_dtls_seq = null"
                    //+ ", record_status = @active_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE money_req_dtls_seq = @money_req_dtls_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
